package com.widget_demos.tszui;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.widget_demos.R;

public class UIActivity extends Activity {
	
	private ViewPager viewPager;
	private ArrayList<View> viewList;
	private View view1, view2;
	private ImageView cursor;
	private int offset, bmpW, current=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tsz_main);
		
		initView();
		initCursor();
	}
	
	
	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		cursor = (ImageView) findViewById(R.id.image_bottom);
		LayoutInflater inflater = getLayoutInflater();
		view1 = inflater.inflate(R.layout.tsz_layout1, null);
		view2 = inflater.inflate(R.layout.tsz_layout2, null);
		viewList = new ArrayList<View>();
		viewList.add(view1);
		viewList.add(view2);
		MyPagerAdapter pagerAdapter = new MyPagerAdapter();
		viewPager.setAdapter(pagerAdapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
//			int one = offset * 2 + bmpW;
			@Override
			public void onPageSelected(int arg0) {
				Animation animation = null;
				switch(arg0){
				case 0:
					if(current == 1){
						animation = new TranslateAnimation(offset, 0, 0, 0);
					}
					break;
				case 1:
					if(current == 0){
						animation = new TranslateAnimation(0, offset, 0, 0);
					}
					break;
				}
				current = arg0;
				if(animation != null){
					animation.setFillAfter(true);
					animation.setDuration(300);
					cursor.startAnimation(animation);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}


	private void initCursor() {
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;
//		offset = (screenWidth/3 - bmpW) / 2;
		offset = screenWidth / 2;
		Matrix m = new Matrix();
//		m.postTranslate(offset, 0);
		m.postScale((float)(offset/bmpW), 1f);
		cursor.setImageMatrix(m);
	}


	class MyPagerAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			
			return arg0 == arg1;
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));
			return viewList.get(position);
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
			super.destroyItem(container, position, object);
		}
		
	}

}
